package vtt.android.lichphatsongbongda.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import vtt.android.library.VttBaseApplication;
import vtt.android.library.utils.VttLog;
import vtt.android.library.utils.api_helper.HTTPRequestor;
import vtt.android.library.utils.api_helper.JSONResponseHandler;
import vtt.android.lichphatsongbongda.entities.TVScheduleResult;
import android.content.Context;

public class TVScheduleAPI {
    
	VttLog log = new VttLog("TVScheduleAPI", VttBaseApplication.isDebuggable);
    //private static final String BASE_URL = "http://172.30.47.66/lpsbongda/";
    private static final String BASE_URL = "http://vlabsmartapps.net84.net/lpsbongda/";
    private static final String ENCODING = "UTF-8";
    private Context mContext;

    private static final ResponseHandler<TVScheduleResult> RESPONSE_HANDLER = new JSONResponseHandler<TVScheduleResult>() {
        @Override
        protected TVScheduleResult handleJSON(HttpEntity entity) throws JSONException, ParseException, IOException {
            String json = EntityUtils.toString(entity, ENCODING);
            JSONObject jsonObject = null;
            jsonObject = new JSONObject(json);
            return new TVScheduleResult(jsonObject);
        }
    };
    
    /**
     * @param context
     */
    public TVScheduleAPI(Context ctx) {
        mContext = ctx;
    }

    private void request(String url, HTTPRequestor requestor, ResponseHandler<?> handler) {
    	log.d("VTT>>> url " + url);
        requestor.setEncoding(ENCODING);
        requestor.setUserAgent(System.getProperty("http.agent"));
        requestor.get(url, handler);
    }

    /**
     * get schedule data
     * @param requestor
     */
    public void getSchedule(HTTPRequestor requestor) {
        request(BASE_URL, requestor, RESPONSE_HANDLER);
    }

}
